package com.jacen.aws.util;

import com.jacen.aws.setting.Setting;

import javax.swing.*;
import java.io.File;

/**
 * 版权声明：Copyright(c) 2018 All Rights Reserved.
 *
 * @Author Jacen (295117485@qq.com)
 * @Date 2018/10/13 8:38
 * @Version 1.0
 * @Description 日志工具类
 */
public class LogUtil {

    public static void printMessage(JTextArea txtAreaLog, String... message) {
        printMessage(txtAreaLog, true, message);
    }

    public static void printMessage(JTextArea txtAreaLog, boolean wirteLog, final String... message) {
        int length = message.length;
        for (int i = 0; i < length; i++) {
            String log = DateUtil.getNowFormatTime(DateUtil.DEFAULT_FORMAT) + " - " + message[i] + "\n";
            txtAreaLog.append(log);
            txtAreaLog.paintImmediately(txtAreaLog.getBounds());
            if (wirteLog) {
                writeLog(log);
            }
        }

    }

    /**
     * 写日志
     *
     * @param log
     */
    public static void writeLog(String log) {
        try {
            String format = DateUtil.getNowFormatTime(DateUtil.SIMPLE_FORMAT);
            String fileName = String.format(Setting.LOG_FILE_PATH, format);
            File logFile = new File(fileName);
            if (!logFile.exists()) {
                logFile.createNewFile();
            }
            //追加日志
            FileUtil.append(fileName, log);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
